package view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import net.miginfocom.layout.AC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
import view.EmployeesPanel.MouseHandler;
import view.EmployeesPanel.TableModel;

import model.Budget;
import model.Employee;
import model.BasicBudget;
import model.FoodServiceBudget;

public class BudgetPanel extends JPanel{
	private Vector<Object[]> data;
	private String[] headers = {"Department", "Budget", "Expenses", "Profit", "Total"};
	private JTable table;
	private Vector<Budget> budgets;
	
	private JLabel totalBudgetLabel;
	private JLabel totalExpensesLabel;
	private JLabel totalProfitLabel;
	private JLabel totalTotalLabel;
	
	public BudgetPanel(){
		data = new Vector<Object[]>();
		budgets = new Vector<Budget>();
		initGUI();
		
		loadBudgetData();
		data = loadTableData();
		repaint();
	}
	
	private void initGUI(){
		this.setLayout(new MigLayout("fillx,insets 25"));  

		table = new JTable();
		table.addMouseListener(new MouseHandler());
		table.setAutoCreateRowSorter(true);
		TableModel model = new TableModel();
		table.setModel(model);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		totalBudgetLabel = new JLabel("---");
		totalExpensesLabel = new JLabel("---");
		totalProfitLabel = new JLabel("---");
		totalTotalLabel = new JLabel("---");
		
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(this.getMaximumSize().width - 50, this.getMaximumSize().height - 200));
		table.setFillsViewportHeight(true);
		this.add(scrollPane,"span");
		
		JPanel totalPanel = new JPanel(new MigLayout("fillx","[90%]100[90%]100[90%]100[90%]100[90%]"));
		totalPanel.add(new JLabel("Total"));

		
		totalPanel.add(totalBudgetLabel);
		totalPanel.add(totalExpensesLabel);
		totalPanel.add(totalProfitLabel);
		totalPanel.add(totalTotalLabel);
		
		this.add(totalPanel);
	}
	
	/**
	 * loads data from budgets vector into another vector to be displayed by the table
	 * @return
	 */
	private Vector<Object[]> loadTableData(){
		Vector<Object[]> v = new Vector<Object[]>();
		
		double totalBudget = 0;
		double totalExpenses = 0;
		double totalProfits = 0;
		double totalTotals = 0;
		
		for(Budget b : budgets){
			v.add(new String[]{b.getDepartment(),Double.toString(b.getBudget()),Double.toString(b.getExpenses()),Double.toString(b.getProfit()),Double.toString(b.getTotal())});
			totalBudget += b.getBudget();
			totalExpenses += b.getExpenses();
			totalProfits += b.getProfit();
			totalTotals += b.getTotal();
		}
		
		totalBudgetLabel.setText(Double.toString(totalBudget));
		totalExpensesLabel.setText(Double.toString(totalExpenses));
		totalProfitLabel.setText(Double.toString(totalProfits));
		totalTotalLabel.setText(Double.toString(totalTotals));
		
		return v;
	}
	
	private void loadBudgetData(){
		budgets = new Vector<Budget>();
		
		budgets.add(new BasicBudget("Security", "Security201111"));
		budgets.add(new BasicBudget("Reception", "Reception201111"));
		budgets.add(new BasicBudget("Maintenance", "Maintenance201111"));
		budgets.add(new BasicBudget("Room Service", "RoomService201111"));
		budgets.add(new BasicBudget("House Keeping", "HouseKeeping201111"));
		budgets.add(new FoodServiceBudget("Food Service", "FoodService201111"));
		/*budgets.add(new Budget("Room Service",25000,21000,34000,13000,15,7500));
		budgets.add(new Budget("Food Service",50000,35000,92400,57400,50,20000));
		budgets.add(new Budget("Reception",5000,3500,0,-3500,3,1000));
		budgets.add(new Budget("Maintenance",20000,19000,0,-19000,3,6000));*/
		
		
	}
	
	/**
	 * Default table model but all cells are not editable
	 * @author dan
	 *
	 */

	class TableModel extends AbstractTableModel {
		public int getColumnCount() {
			return headers.length;
		}
		public int getRowCount() {
			return data.size();
		}
		public String getColumnName(int col) {
			return headers[col];
		}
		public Object getValueAt(int row, int col) {
			return data.get(row)[col];
		}
		public boolean isCellEditable(int row, int col) {
			return false;
		}
	}
	
	public class MouseHandler implements MouseListener{

		public void mouseClicked(MouseEvent arg0) {

			if(arg0.getClickCount() >= 2){	
				int row = table.getSelectedRow();
				int col = table.getSelectedColumn();
				if(row == -1 || col == -1) return;

				row = table.convertRowIndexToModel(row);
				col = table.convertColumnIndexToModel(col);
				
				String name = (String)data.get(row)[0];
				
				Budget b = budgets.get(row);
				b.showInfo(BudgetPanel.this);
			}
		}



		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}

		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}

		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}

		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}
	}
	
	public static void main(String...args){

		JFrame f = new JFrame();

		f.setSize(new Dimension(700,500));

		f.add(new BudgetPanel());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
}
